//配置路由的地方
import Vue from 'vue'
import VueRouter from 'vue-router'

//使用路由
Vue.use(VueRouter)

//引入路由组件
import Home from '@/pages/Home'
import Search from '@/pages/Search'
import Login from '@/pages/Login'
import Register from '@/pages/Register'

//保存原始的push和replace
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;

//重写push|replace
/* location:跳转的位置
  resolve:成功回调
  reject: 失败的回调

  call和apply的区别
      相同点：都可以调用函数一次，都可以篡改函数的上下文一次
      不同点：call和apply传递参数，call传递参数用逗号隔开，apply方法执行传递数组'ppls;'d,

 */
//解决编程式路由跳转，多次执行会抛出NavigationDuplicated的警告错误
VueRouter.prototype.push = function(location,resolve,reject){
  if(resolve && reject){
    originPush.call(this,location,resolve,reject);

  }else{
    originPush.call(this,location,()=>{},()=>{})
  }
}
VueRouter.prototype.replace = function(location,resolve,reject){
  if(resolve && reject){
    originReplace.call(this,location,resolve,reject);

  }else{
    originReplace.call(this,location,()=>{},()=>{})
  }
}
//配置路由
export default new VueRouter({
  //配置路由
  routes:[
    {
      path:'/home',
      component:Home,
      meta:{show:true}
    },
    {
      path:'/search/:keyword?', //代表keyword可传可不传
      component: Search,
      meta:{show:true},
      name:'search'
    },
    {
      path:'/login',
      component:Login,
      meta:{show:false}
    },
    {
      path:'/register',
      component:Register,
      meta:{show:false}
    },
    //重定向，在项目跑起来的时候，访问/,立马让他定向到首页
    {
      path:'*',
      redirect:"/home"
    }
  ]

})